<?php

namespace App\Http\Controllers\background;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Excel;
use App\Models\Mysql\{XnSupplierGoods,XuGoodsBrand};
use App\Services\Brand\BrandService;
use Illuminate\Http\Request;
class BrandController extends Controller
{
    protected $brandService;
    public function __construct(BrandService $brandService)
    {
        $this->brandService = $brandService;
    }
    // 获取品牌分类数据
    public function excelinfo(Request $request){

        Excel::create('aaa',function($excel){
            $excel -> sheet('Sheet1',function($sheet){
                $sheet -> row(1,['姓名','年龄','性别']);
                $sheet -> row(1,['小命','年龄','性别']);
                $sheet -> row(1,['姓名','年龄','性别']);
                $sheet -> row(1,['姓名','年龄','性别']);
            });
        })-> store('xls','/www/wwwroot/');
    }
    // 获取品牌分类数据
    public function brandlist(Request $request){
        $type = $request-> post('type');
       //echo 1111;exit;
        $res = $this->brandService ->get($type);
        $ress = $this -> make_tree($res);
        return successly($ress);
    }

    function make_tree($list,$pk='category_id',$pid='pid',$child='son',$root=0){
        $tree=array();
        $packData=array();
        foreach ($list as $data) {
            $packData[$data[$pk]] = $data;
        }
        foreach ($packData as $key =>$val){
            if($val[$pid]==$root){//代表跟节点
                $tree[]=&$packData[$key];
            }else{
                //找到其父类
                $packData[$val[$pid]][$child][]=&$packData[$key];
            }
        }
        return $tree;
    }
    // 添加分类数据
    public function addbrandinfo(Request $request){
        $info = $request-> post();
        if (empty($info['pid'])){
            $info['pid'] = 0;
        }
        if ($info['pid'] == 0) {
            $level = 1;
        } else {
            $level_list = XuGoodsBrand:: where('category_id',$info['pid']) -> value('level');
            $level = $level_list+1;
        }
        if($this -> brandService ->create($info['code'],$info['pid'],$level,$info['sort'])){
            return successly();
        }else{
            return fails('添加失败');
        };
    }
    //产品分类删除
    public function branddel(Request $request){
        $id =  $request-> post('id');
        //验证此分类下是否有商品
        $goodsInfo1=XnSupplierGoods::where('brand_id','=',$id)->exists();
        $goodsInfo2=XuGoodsBrand::where('pid','=',$id)->exists();
        if($goodsInfo1 || $goodsInfo2){
            return fails('该分类下有商品或子分类');
        }
        if($this -> brandService -> delete($id)){
            return successly();
        }else{
            return fails('删除失败');
        }
    }
    public function getsuppbrand(Request $request){
            $brand_id = $request -> post('brand_id');
            $brand_name = $request -> post('brand_name');
            $cate_info = $this -> brandService -> all($brand_id);
            $arr = [
                'category_id' => $brand_id,
                'category_name' => $brand_name,
                'son' => $cate_info
            ];
         return successly($arr);
    }
    public function gettopbrand(Request $request){
        $data = XuGoodsBrand:: get();
        $one_list = $this -> demo11($data,0,0);
        return successly($one_list);
    }

    // 处理分类类目以便于后台前台展示处理
    function demo11($arr,$id,$level)
    {
        static $list = array();
        foreach ($arr as $k => $v) {
            if ($v['pid'] == $id) {
                $v['level'] = $level;
                $list[] = $v;
                $this -> demo11($arr, $v['category_id'], $level + 1);
            }
        }
        return $list;
    }

    public function editbrand(Request $request){
        $brand_id = $request -> post();
        if(!isset($brand_id['brand_id'])){
            return fails('请传参');
        }
        $update = [];
        if(isset($brand_id['brand_name'])){
            $update['category_name'] = $brand_id['brand_name'];
        }
        if(isset($brand_id['sort'])){
            $update['sort'] = $brand_id['sort'];
        }
        if(!empty($update)){
            XuGoodsBrand::where('category_id','=',$brand_id['brand_id'])->update($update);
        }
        return successly();
    }

    public function getMertBrandinfo(){
        $infos =  XuGoodsBrand::whereIn('level',[1,2])
            ->select('category_id','category_name','pid','sort')
            -> orderBy('sort','asc')
            -> get()
            ->toArray();
       // dd($infos);
        $info = $this ->  make_tree($infos);
        return successly($info);
    }
    public function brandLevelList(Request $request){
        $type = $request -> post('type',''); //分类级数
        $brandId = $request -> post('brand_id'); //分类id
        if($type == 1){
            $infos =  XuGoodsBrand::where('level',1)
                ->select('category_id','category_name','pid','sort')
                -> orderBy('sort','asc')
                -> get()
                -> toArray();
        }else{
            $infos =  XuGoodsBrand::where('pid',$brandId)
                ->select('category_id','category_name','pid','sort')
                -> orderBy('sort','asc')
                -> get()
                -> toArray();
        }
        return successly($infos);
    }
}
